package com.middlesummer.yuxiaoshuang.strategy.impl;

import com.middlesummer.yuxiaoshuang.strategy.ImageSequenceSwitch;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.util.ArrayDeque;
import java.util.Queue;

/**
 * @author：maolan
 * @date：2022/5/4 11:29
 * @describe: 不依赖图片名称顺序切换实现类
 */
public class ImageNameSequenceSwitchImpl implements ImageSequenceSwitch {

    private Logger logger = LoggerFactory.getLogger(ImageNameSequenceSwitchImpl.class);

    private Integer number = 101;
    private Integer successUpdateNumber = 0;
    private Integer errorUpdateNumber = 0;

    @Override
    public void sequenceSwitch(String fileDirectoryPath) {
        long start = System.currentTimeMillis();
        File imageDirectoryFile = new File(fileDirectoryPath);
        if (imageDirectoryFile.isDirectory()) {
            File[] imagesFile = imageDirectoryFile.listFiles();
            for (File file : imagesFile) {
                String fileAllName = file.getName();
                String[] fileNameArray = fileAllName.split("\\.");
                try {
                    String fileNewDirectoryPath = fileDirectoryPath.substring(0, fileDirectoryPath.lastIndexOf("source"));
                    String fileNewAllName = fileNewDirectoryPath + "target\\" + number++ + "." + fileNameArray[1];
                    file.renameTo(new File(fileNewAllName));
                    successUpdateNumber++;
                } catch (Exception e) {
                    logger.error("此文件：{} 处理失败，失败原因：{}", fileAllName, e.getMessage());
                    errorUpdateNumber++;
                }
            }
        }
        long end = System.currentTimeMillis();
        logger.info("文件处理完成，成功处理文件数：{}个，失败处理文件数：{}个，总用时：{} ms",successUpdateNumber, errorUpdateNumber, end - start);
    }
}
